<template>
  <ul class="catagtory">
    <li
      :class="{ select: index === currIndex }"
      v-for="(item, index) in tabList"
      :key="item.id"
      @click="liClick(index, item.id)"
    >
      {{ item.name }}
    </li>
  </ul>
</template>
<script>
import { mapState } from 'vuex'
export default {
  computed: {
    ...mapState('catagtory', ['tabList', 'currIndex'])
  },
  async created () {
    console.log(1)
    // 在获取完tab列表数据后再获取详情数据,不然取不到第一条数据的id
    await this.$store.dispatch('catagtory/getTabList')
    console.log(2)
    this.$store.dispatch(
      'list/getNewsList',
      this.$store.state.catagtory.tabList[0].id
    )
  },
  methods: {
    liClick (index, id) {
      this.$store.commit('catagtory/setCurrIndex', index)
      // 获取详情
      this.$store.dispatch('list/getNewsList', id)
    }
  }
}
</script>
<style></style>
